package org.example.task3;


import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Table;


public class DeleteRow {
    public static Configuration configuration;
    public static Connection connection;
    public static Admin admin;
    public static void init(){//建立连接
        configuration = HBaseConfiguration.create();
        configuration.set("hbase.rootdir","hdfs://node1:8020/hbase");
        try{
            connection = ConnectionFactory.createConnection(configuration);
            admin = connection.getAdmin();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    public static void close(){//关闭连接
        try{
            if(admin != null){
                admin.close();
            }
            if(connection != null){
                connection.close();
            }
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    public static void deleteRow(String tableName,String row) throws IOException{
        init();
        Table table = connection.getTable(TableName.valueOf(tableName));
        Delete delete = new Delete(row.getBytes());
        table.delete(delete);
        close();
    }
    public static void main(String[] args){
        try{
            deleteRow("test","2");
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}


